home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / tcpprobe.c < prev    next >
C/C++ Source or Header  |  1998-07-17  |  1KB  |  59 lines

  1. /* -*-C-*- tcpprobe.c */
  2. /* tcpprobe - report on which tcp ports accept connections */
  3. /* IO ERROR, error@axs.net, Sep 15, 1995 */
  4.  
  5. #include <stdio.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <errno.h>
  9. #include <netdb.h>
  10. #include <signal.h>
  11.  
  12. int main(int argc, char **argv)
  13. {
  14.   int probeport = 0;
  15.   struct hostent *host;
  16.   int err, i, net;
  17.   struct sockaddr_in sa;
  18.  
  19.   if (argc != 2) {
  20.     printf("Usage: %s hostname\n", argv[0]);
  21.     exit(1);
  22.   }
  23.  
  24.   for (i = 1; i < 1024; i++) {
  25.     strncpy((char *)&sa, "", sizeof sa);
  26.     sa.sin_family = AF_INET;
  27.     if (isdigit(*argv[1]))
  28.       sa.sin_addr.s_addr = inet_addr(argv[1]);
  29.     else if ((host = gethostbyname(argv[1])) != 0)
  30.       strncpy((char *)&sa.sin_addr, (char *)host->h_addr, sizeof sa.sin_addr);
  31.     else {
  32.       herror(argv[1]);
  33.       exit(2);
  34.     }
  35.     sa.sin_port = htons(i);
  36.     net = socket(AF_INET, SOCK_STREAM, 0);
  37.     if (net < 0) {
  38.       perror("\nsocket");
  39.       exit(2);
  40.     }
  41.     err = connect(net, (struct sockaddr *) &sa, sizeof sa);
  42.     if (err < 0) {
  43.       printf("%s %-5d %s\r", argv[1], i, strerror(errno));
  44.       fflush(stdout);
  45.     } else {
  46.       printf("%s %-5d accepted.                               \n", argv[1], i);
  47.       if (shutdown(net, 2) < 0) {
  48.     perror("\nshutdown");
  49.     exit(2);
  50.       }
  51.     }
  52.     close(net);
  53.   }
  54.   printf("                                                                \r");
  55.   fflush(stdout);
  56.   return (0);
  57. }
  58.  
  59.